﻿using PEPe.DataAccessModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PEPe.ADO
{
    public class ConsultaADO
    {
        public List<Consulta> ConsultarTodos()
        {
            using (var db = new PEPeEntities())
            {
                return db.Consulta.OrderBy(x => x.Descricao).ToList();
            }

        }

        public Consulta ConsultarByAgenda(int agendaId)
        {
            using (var db = new PEPeEntities())
            {
                return db.Consulta.Where(x=>x.Agenda.Any(y=>y.Id==agendaId)).OrderBy(x => x.Descricao).FirstOrDefault();
            }

        }


        public void Salvar(Consulta consulta)
        {
            using (var db = new PEPeEntities())
            {
                if (consulta.Id == 0)
                {
                    db.Consulta.Add(consulta);
                }
                else
                {
                    Consulta upd = db.Consulta.Find(consulta.Id);
                    db.Entry(upd).CurrentValues.SetValues(consulta);
                }
                db.SaveChanges();
            }
        }

    }
}
